package qeb.handheldmedical.patient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import qeb.handheldmedical.R;
import qeb.handheldmedical.adapter.SlideListView;
import qeb.handheldmedical.adapter.BloodSugarAdapter;
import qeb.handheldmedical.helper.WSHelper;
import qeb.handheldmedical.model.BloodSugar;
import qeb.handheldmedical.model.InPatient;
import qeb.handheldmedical.model.User;

public class BloodSugarList extends Activity {
    private SlideListView slideListView;
    private BloodSugarAdapter adapter;
    InPatient ip = new InPatient();
    User usr = new User();
    BloodSugar bloodSugar;
    private JSONArray xtJsonArray;

    private Button btn_title_back;
    private Button btn_title_right;
    private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        ip = (InPatient) getIntent().getSerializableExtra("patientinfo");
        usr = (User) getIntent().getSerializableExtra("userinfo");
        setContentView(R.layout.query_bloodsugar);
        initView();
        dataBind();
    }

    private void initView() {
        slideListView = (SlideListView) findViewById(R.id.slideListView3);
        tv_title = (TextView) findViewById(R.id.tv_top_title);
        tv_title.setText(ip.XM + "的血糖");
        btn_title_back = (Button) findViewById(R.id.btn_title_back);
        btn_title_back.setOnClickListener(MyOnClickListener);
        btn_title_right = (Button) findViewById(R.id.btn_title_right);
        btn_title_right.setText("新增");
        btn_title_right.setOnClickListener(MyOnClickListener);
    }

    /* 系统主菜单点击事件（在院病人、门诊病人等） */
    private OnClickListener MyOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO 自动生成的方法存根
            // Bundle bundle=new Bundle();
            switch (v.getId()) {
                case R.id.btn_title_back:// 返回按钮
                    BloodSugarList.this.finish();
                    break;
                case R.id.btn_title_right:
                    bloodSugar = null;
                    Intent intent = new Intent(BloodSugarList.this,
                            qeb.handheldmedical.patient.AddBloodSugar.class);
                    intent.putExtra("patientinfo", ip);
                    intent.putExtra("userinfo", usr);
                    intent.putExtra("bloodSugar", bloodSugar);
                    BloodSugarList.this.startActivityForResult(intent, 1);
                    break;
                default:
                    break;
            }
        }
    };

    private void dataBind() {
        xtJsonArray = WSHelper.GetXtList(ip.ZYXH);
        if (xtJsonArray != null && xtJsonArray.length() > 0) {
            adapter = new BloodSugarAdapter(xtJsonArray.length(),
                    BloodSugarList.this, xtJsonArray);

            slideListView.setAdapter(adapter);

            slideListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> tw, View view,
                                        int position, long id) {
                    // TODO Auto-generated method stub
                    JSONObject xtObject;
                    bloodSugar = new BloodSugar();
                    try {
                        xtObject = xtJsonArray.getJSONObject(position);
                        bloodSugar.getValue(xtObject);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    Intent intent = new Intent(BloodSugarList.this,
                            qeb.handheldmedical.patient.AddBloodSugar.class);
                    intent.putExtra("patientinfo", ip);
                    intent.putExtra("userinfo", usr);
                    intent.putExtra("bloodSugar", bloodSugar);
                    BloodSugarList.this.startActivityForResult(intent, 1001);
                    // Temperature.this.getApplicationContext().startActivity(
                    // intent);

                }
            });

        }
    }

    // 接收新的Activiity 返回的结果

    /**
     * requestCode:10 resultCode:100
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 1001) {
                if (data != null) {
                    Bundle b = data.getExtras();
                    if (b != null) {
                        if ("1".equals(b.getString("UPDATE"))) {
                            dataBind();
                        }
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
